package me.keiwu.user.controller;

import com.alibaba.fastjson.JSONObject;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.enums.NoticeType;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.user.service.NoticeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by kei on 05/02/2018.
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @RequestMapping("list")
    public ResponseV2 list(HttpServletRequest request, Integer noticeType,
                           @RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer size) {
        Long userId = ContextUtil.getLoginUserId(request);
        NoticeType type = NoticeType.parse(noticeType);
        return ResponseV2.ok(noticeService.list(userId, type, page, size));
    }


    @RequestMapping("read")
    public ResponseV2 read(Long noticeId) {
        return ResponseV2.ok(noticeService.read(noticeId));
    }


    @RequestMapping("read/multi")
    public ResponseV2 readMulti(String noticeIdList) {
        List<Long> list = new ArrayList<>();
        for (String s : StringUtils.split(noticeIdList, ",")) {
            s = StringUtils.trim(s);
            if (StringUtils.isBlank(s)) continue;
            list.add(Long.valueOf(s));
        }
        return ResponseV2.ok(noticeService.readMulti(list));
    }



    @RequestMapping("redPoint")
    @ResponseBody
    public ResponseV2 redPoint(HttpServletRequest request) {
        Long userId = ContextUtil.getLoginUserId(request);
        JSONObject json = new JSONObject();
        json.put("stealGold", noticeService.checkNotRead(NoticeType.STEAL_GOLD.getType(), userId));
        json.put("comment", noticeService.checkNotRead(NoticeType.COMMENT.getType(), userId));
        json.put("challenge", noticeService.checkNotRead(NoticeType.CHALLENGE.getType(), userId));
        return ResponseV2.ok(json);
    }

}
